package com.cqc.listviewchoicedemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ListViewCompat;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${cqc} on 2017/3/13.
 */

public class BgSingleChoiceActivity extends AppCompatActivity {

    private static final String TAG = "BgSingleChoice";
    private ListView listView;
    private Button btn;
    private List<String> dataList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_choice);
        initData();
        findViews();
        initViews();
    }

    private void initData() {
        for (int i = 0; i < 100; i++) {
            dataList.add("item" + i);
        }
    }

    private void initViews() {
        //设置单选模式有2种方法：xml和java设置
//        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setAdapter(new MyAdapter());
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                String text = (String) listView.getItemAtPosition(position);
                Log.d(TAG, "text=" + text);
            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String selectedItem = (String) listView.getSelectedItem();//无效
                int checkedItemCount = listView.getCheckedItemCount();//总数量，单选模式是1
                long[] checkItemIds = listView.getCheckItemIds();//选中的item的id,adapter中的getItemId(int position)，单选模式只有一个，checkItemIds[0]
                long[] checkedItemIds = listView.getCheckedItemIds();
                int checkedItemPosition = listView.getCheckedItemPosition();//选中的item的position

                Log.d(TAG, "selectedItem=" + selectedItem);
                Log.d(TAG, "checkedItemCount=" + checkedItemCount);
                Log.d(TAG, "checkItemIds[0]=" + checkItemIds[0]);
                Log.d(TAG, "checkedItemIds[0]=" + checkedItemIds[0]);
                Log.d(TAG, "checkedItemPosition=" + checkedItemPosition);
            }
        });
    }

    private void findViews() {
        listView = (ListView) findViewById(R.id.listView);
        btn = (Button) findViewById(R.id.btn);
    }


    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public Object getItem(int position) {
            return dataList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            MyViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_single_choice, viewGroup, false);
                holder = new MyViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (MyViewHolder) convertView.getTag();
            }
            holder.tv_item_single_choice.setText(dataList.get(position) + "");
            return convertView;
        }

        // 要想使用getCheckedItemIds()，必须返回true
        @Override
        public boolean hasStableIds() {
            return true;
        }
    }

    public class MyViewHolder {
        TextView tv_item_single_choice;

        public MyViewHolder(View itemView) {
            tv_item_single_choice = (TextView) itemView.findViewById(R.id.tv_item_single_choice);
        }
    }
}
